package com.slz3.business.module.wmsManagement.wmsWarehouseManage.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.wmsManagement.wmsWarehouseManage.domain.WareTypeEnum;
import com.slz3.business.module.wmsManagement.wmsWarehouseManage.domain.WmsWarehouseManage;
import com.slz3.business.module.wmsManagement.wmsWarehouseManage.domain.WmsWarehouseManageFactory;
import com.slz3.business.module.wmsManagement.wmsWarehouseManage.steward.WmsWarehouseManageSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class WmsWarehouseManageServiceImpl implements WmsWarehouseManageService {
    @Override
    public WmsWarehouseManage save(WmsWarehouseManage wmsWarehouseManage) {
        if (wmsWarehouseManage.getUpper() != null){
            wmsWarehouseManage.setSerWmsType(WareTypeEnum.KU_QU);
        }else{
            wmsWarehouseManage.setSerWmsType(WareTypeEnum.CANG_KU);
        }
        return Repositorys.wmsWarehouseManage.save(WmsWarehouseManageFactory.initBaseFields(wmsWarehouseManage));
    }

    @Override
    public Page<WmsWarehouseManage> page(MultiValueMap<String, String> params) {
        return Repositorys.wmsWarehouseManage.findAll(
                WmsWarehouseManageSpecification.findJoin(params), PageableSteward.extract(params)
        );
    }


    @Override
    public WmsWarehouseManage modify(WmsWarehouseManage wmsWarehouseManageNew) {
        return Repositorys.wmsWarehouseManage.saveAndFlush(WmsWarehouseManageFactory.initBaseFields(wmsWarehouseManageNew));
    }

    @Override
    public Optional<WmsWarehouseManage> single(UUID uuid) {
        return Repositorys.wmsWarehouseManage.findById(uuid);
    }

    @Override
    public List<WmsWarehouseManage> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.wmsWarehouseManage.findAllById(uuids);
    }

    @Override
    public List<WmsWarehouseManage> delete(List<WmsWarehouseManage> wmsWarehouseManageList) {
        wmsWarehouseManageList.forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            WmsWarehouseManageFactory.initBaseFields(item);
        });
        return Services.wmsWarehouseManageTransactional.batchModify(wmsWarehouseManageList);
    }

    @Override
    public List<WmsWarehouseManage> getHouseAndArea() {
        return Repositorys.wmsWarehouseManage.findHouseAndArea();
    }


}
